package com.bear.auction.client.util;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class JSONArrayAdapter extends BaseAdapter{
	private Context ctx;
	private JSONArray jsonArray;
	private String property;
	private boolean hasIcon;

	public JSONArrayAdapter(Context ctx, JSONArray jsonArray,
			String string, boolean b) {
		this.ctx = ctx;
		this.jsonArray = jsonArray;
		this.property = string;
		this.hasIcon = hasIcon;
	}

	@Override
	public int getCount() {
		return jsonArray.length();
	}

	@Override
	public Object getItem(int position) {
		return jsonArray.optJSONObject(position);
	}

	@Override
	public long getItemId(int position) {
		try
		{
			return((JSONObject)getItem(position)).getInt("id");
		}catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		LinearLayout linear = new LinearLayout(ctx);
		linear.setOrientation(0);
		TextView tv = new TextView(ctx);
		try{
			String itemName = ((JSONObject)getItem(position)).getString(property);
			tv.setText(itemName);
		}catch (Exception e) {
			e.printStackTrace();
		}
		tv.setTextSize(20);
		linear.addView(tv);
		return linear;
	}

}
